# -*- coding: utf-8 -*-
import scrapy
from lxml import etree
from scrapy.selector import Selector
from qiangchengwuyou.items import QiangchengwuyouItem


class A51jobspiderSpider(scrapy.Spider):
    name = '51jobSpider'
    allowed_domains = ['51job.com']
    print("< 每回车一次就打印一条 >")
    job = input("你要找什么工作：")
    isArea = input("想不想在南山区先(3为南山，0为全市): ")
    url = "http://search.51job.com/list/040000%252C00,040" + isArea + "00,0000,00,9,99," + job + ",2,{}.html"
    urls = []
    for i in range(1, 10):
        url_ = url.format(str(i))
        urls.append(url_)
    start_urls = urls

    def parse(self, response):
        list_ = response.xpath('//*[@id="resultList"]/div[@class="el"]')
        for i in list_:
            item = get_list_single_info(i)
            yield scrapy.Request(item['job_info_url'], callback=self.get_job_detail_address, meta={'meta_1': item})


    '''
    拿职位详情页的上班地址
    '''
    def get_job_detail_address(self, response):
        item = response.meta['meta_1']
        address = (response.xpath('//div[@class="bmsg inbox"]/p/text()').extract()[1]).strip()
        job_51 = get_info_integration(item, address)
        print("\n=================================================\n")
        print(job_51)
        input("")


'''
搜索页解析出来的list对象要从中取出我所需要的数据
'''
def get_list_single_info(i):
    item = {}
    item['job_name'] = i.xpath("./p/span/a/@title").extract_first()
    item['company_name'] = i.xpath("./span[1]/a/@title").extract_first()
    item['salary'] = i.xpath("./span[3]/text()").extract_first()
    item['publish_time'] = i.xpath("./span[4]/text()").extract_first()
    item['job_info_url'] = i.xpath("./p/span/a/@href").extract_first()
    return item


'''
整合成对象数据阿喂
'''
def get_info_integration(item, address):
    job_51 = QiangchengwuyouItem()
    job_51['job_name'] = item['job_name']
    job_51['company_name'] = item['company_name']
    job_51['salary'] = item['salary']
    job_51['publish_time'] = item['publish_time']
    job_51['job_info_url'] = item['job_info_url']
    job_51['address'] = address
    return job_51